package implementor;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import dao.StockInDao;
import dbhandelling.Connections;
import dbhandelling.Crude;
import dto.Stock;



public class StockInImplementor extends Crude implements StockInDao
{
	String className=new Stock().getClass().getName();
	@Override
	public void add(Stock materialMaster) throws Exception 
	{
		insert(materialMaster);
	}

	@Override
	public void update(Stock materialMaster) throws Exception 
	{
		super.update(className, "ginNo", materialMaster.getGinNo()+"","autoStockCode", materialMaster.getAutoStockCode()+"");
		super.update(className, "storeCode", materialMaster.getStoreCode()+"","autoStockCode", materialMaster.getAutoStockCode()+"");
		super.update(className, "mCode", materialMaster.getmCode(),"autoStockCode", materialMaster.getAutoStockCode()+"");
		super.update(className, "mquant", materialMaster.getMquant()+"","autoStockCode", materialMaster.getAutoStockCode()+"");
		super.update(className, "specCode", materialMaster.getSpecCode()+"","autoStockCode", materialMaster.getAutoStockCode()+"");
		
		
	}

	@Override
	public void delete(Stock materialMaster) throws Exception 
	{
		delete(className, "autoStockCode", materialMaster.getAutoStockCode()+"");
		
	}

	@Override
	public Stock retreive(String conditionField, String ConditionValue) throws Exception 
	{
		Stock materialMaster=(Stock) super.retreive(className,conditionField, ConditionValue);
		return materialMaster;
	}

	@Override
	public Stock[] retreiveArray(String conditionField, String conditionValue) throws Exception 
	{
		Stock[] materialMasters;
		Object[] obj=super.retreiveArrayOnCondition(className, conditionField, conditionValue);
		materialMasters=new Stock[obj.length];
		
		for(int i=0;i<materialMasters.length;i++)
		{
			materialMasters[i]=(Stock) obj[i];
		}
		
		return materialMasters;
	}

	@Override
	public Stock[] retreiveAll() throws Exception 
	{
		Stock[] materialMasters;
		
		Object[] obj=super.retreiveAll(className);
		
		materialMasters=new Stock[obj.length];
		
		for(int i=0;i<materialMasters.length;i++)
		{
			materialMasters[i]=(Stock) obj[i];
		}
		
		return materialMasters;
	}

	@Override
	public double getStockQty(String mCode, int specCode) throws SQLException {
	      final String sql = " SELECT SUM(mQuant) FROM STOCK WHERE mCode = "+"'"+mCode+"'"+" AND"
		+" specCode = "+specCode;
	      Statement st = Connections.getMyStatement();
	      ResultSet rs=st.executeQuery(sql);
	      rs.next();
	      return rs.getDouble(1);
	     
	}

}
